﻿/*
   Copyright 2011 Softisis

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using FOM.Interfaces;

namespace FOM.Impl.CompactExtensions
{
    public static class ShortNameCompactManager
    {
        /// <summary>
        /// Compact version of method RegisterMapping
        /// Register a new mapping by it's name, from a type TSrc to type TDst
        /// </summary>
        /// <typeparam name="TSrc">Type of source</typeparam>
        /// <typeparam name="TDst">Type of destination</typeparam>
        /// <param name="mngr">Mappings manager</param>
        /// <param name="name">Mapping name, unci, to be recalled</param>
        /// <returns>The top mapping node</returns>
        public static IMappingNode<TSrc, TDst> RM<TSrc, TDst>(
            this IManager mngr,
            String name)
        {
            return mngr.RegisterMapping<TSrc, TDst>(name);
        }


        /// <summary>
        /// Compact version of method RegisterMapping
        /// Register a new anonymous mapping. Name is unically generated like Anonymous_[xxx], where [xxx] is an int.
        /// </summary>
        /// <typeparam name="TSrc">Type of source</typeparam>
        /// <typeparam name="TDst">Type of destination</typeparam>
        /// <param name="mngr">Mappings manager</param>
        /// <returns>The top mapping node</returns>
        public static IMappingNode<TSrc, TDst> RM<TSrc, TDst>(this IManager mngr)
        {
            return mngr.RegisterMapping<TSrc, TDst>();
        }


        /// <summary>
        /// Compact version of method RegisterMapping
        /// Register a new mapping by it's name, for a type (source and desta have the same type).
        /// </summary>
        /// <typeparam name="T">Source and destination type</typeparam>
        /// <param name="name">Mapping name, unci, to be recalled</param>
        /// <param name="mngr">Mappings manager</param>
        /// <returns>A link to the mapping</returns>
        public static IMappingNode<T, T> RM<T>(this IManager mngr, String name)
        {
            return mngr.RegisterMapping<T, T>(name);
        }


        /// <summary>
        /// Compact version of method RegisterMapping
        /// Register a new anonymous mapping on two same type.
        /// Name is unically generated like Anonymous_[xxx], where [xxx] is an int.
        /// </summary>
        /// <typeparam name="T">Source and destination type</typeparam>
        /// <param name="mngr">Mappings manager</param>
        /// <returns>A link to the mapping</returns>
        public static IMappingNode<T, T> RM<T>(this IManager mngr)
        {
            return mngr.RegisterMapping<T>();
        }


        /// <summary>
        /// Compact version of method unRegisterMapping
        /// Unregister a mapping by its name. If it does not exists, return false,
        /// otherwise true is deletion is ok.
        /// </summary>
        /// <param name="mngr">Mappings manager</param>
        /// <param name="name">Mapping name</param>
        public static bool UM(this IManager mngr, String name)
        {
            return mngr.UnRegisterMapping(name);
        }


        /// <summary>
        /// Compact version of method GetMapping
        /// Retrieve a mapping by its name
        /// </summary>
        /// <typeparam name="TSrc">Source type</typeparam>
        /// <typeparam name="TDest">Destination type</typeparam>
        /// <param name="mapname">Mapping name</param>
        /// <param name="mngr">Mappings manager</param>
        /// <returns>A mapping implementation if found, null otherwise.</returns>
        public static IMappingNode<TSrc, TDest> GM<TSrc, TDest>(
            this IManager mngr,
            String mapname)
        {
            return mngr.GetMapping<TSrc, TDest>(mapname);
        }


        /// <summary>
        /// Compact version of method ApplyMapping
        /// Apply the nammed mapping.
        /// </summary>
        /// <param name="name">name of the mapping. If not exist, Exception is raised.</param>
        /// <param name="src">the source object.</param>
        /// <param name="mngr">Mappings manager</param>
        /// <param name="target">The target object</param>
        public static object AM(
            this IManager mngr,
            String name, object src, object target)
        {
            return mngr.ApplyMapping(name, src, target);
        }


        /// <summary>
        /// Compact version of method ApplyMapping
        /// Apply the mapping pointed by rootNode
        /// </summary>
        /// <param name="rootNode">The root node of the mapping</param>
        /// <param name="src">the source object.</param>
        /// <param name="target">The target object</param>
        /// <param name="mngr">Mappings manager</param>
        /// <param name="objcache">Provide the cache object used with NewInstance(true).
        /// If null, a default one is create, local to the applied mapping call.</param>
        public static object AM(
            this IManager mngr,
            INode rootNode, object src, object target, 
            IDictionary<object, object> objcache)
        {
            return mngr.ApplyMapping(rootNode, src, target, objcache);
        }

        /// <summary>
        /// Compact version of method ApplyMapping
        /// Apply the nammed mapping. because the target is not givent, it's instanciated, according to
        /// root node parameters (newInstance, overriding type, etc ...)
        /// </summary>
        /// <param name="name">name of the mapping. If not exist, Exception is raised.</param>
        /// <param name="src">the source object.</param>
        /// <param name="mngr">Mappings manager</param>
        public static object AM(
            this IManager mngr,
            String name, object src)
        {
            return mngr.ApplyMapping(name, src);
        }


        /// <summary>
        /// Compact version of method ApplyMapping
        /// Apply the mapping staring from this node. because the target is not given, it's instanciated, according to
        /// root node parameters (newInstance, overriding type, etc ...)
        /// </summary>
        /// <param name="rootNode">Root node for mapping</param>
        /// <param name="src">the source object.</param>
        /// <param name="mngr">Mappings manager</param>
        /// <param name="objcache">Provide the cache object used with NewInstance(true).
        /// If null, a default one is create, local to the applied mapping call.</param>
        public static object AM(
            this IManager mngr,
            INode rootNode, object src,
            IDictionary<object, object> objcache)
        {
            return mngr.ApplyMapping(rootNode, src, objcache);
        }
    }
}
